<?php
/**
 * users
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2007-08-06
 * @package rail
 */

session_start();

require_once( '../../railadmin.conf.php' );
require_once( '../../' . $cfg_pahToPHPrail . '/rail_connector.class.php' );

define('RAILUSER_REVISION', '');
define('RAILUSER_VERSION', '1.0');

$rail = new rail_connector( $_SESSION["mysql_db"], $_SESSION["mysql_user"], $_SESSION["mysql_passwd"], $_SESSION["mysql_host"] );
$rail->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);

$smarty = new rail_smarty();
$smarty->caching = false;


/* ------------------------------------ Listener -------------------------------------- */



/* --------------------------------------- Users -------------------------------------- */
if( isset($_REQUEST['user']) )
	$current_user = rail_factory::get_object( $rail, $_REQUEST['user'] );

$rail_users = array();
$data = $rail->send_request( "SELECT `id` FROM `objects` WHERE `type`='RAIL_USER'" );
while( $data_user = $data->fetch_assoc() ){
	$user = rail_factory::get_object( $rail, $data_user['id'] );
	if( !isset($current_user) ) $current_user = $user;
	$rail_users[] = array(
		'id' => $user->get_id(),
		'name' => $user->get_name(),
		'username' => $user->get_attribute( 'username' )
	);
}
$smarty->assign( array(
	'rail_users' => $rail_users,
	'current_user' => array(
		'id' => $current_user->get_id(),
		'name' => $current_user->get_name(),
		'path' => $current_user->get_path(),
		'username' => $current_user->get_attribute( 'username' ),
		'mail' => $current_user->get_attribute( 'mail' ),
		'time_create' => date( 'D d.m.Y H:i', $current_user->get_attribute( OBJ_TIME_CREATE ) ),
		'time_lastwrite' => date( 'D d.m.Y H:i', $current_user->get_attribute( OBJ_TIME_LASTWRITE ) )
	)
));

// all memberships
$memberships = array();
foreach( $current_user->get_memberships() as $group ){
	$memberships[] = array(
		'id' => $group->get_id(),
		'name' => $group->get_name(),
		'is_current_users_group' => $group->get_id() == $current_user->get_group()->get_id()
	);
}
$smarty->assign( 'memberships', $memberships );

// all available groups
$groups_available = array();
$data = $rail->send_request( "SELECT `id` FROM `objects` WHERE `type`='RAIL_GROUP'" );
while( $data_group = $data->fetch_assoc() ){
	$group = rail_factory::get_object( $rail, $data_group['id'] );

	// check wheater this group is already in current users memberships
	foreach( $memberships as $array ){
		if($array['id'] == $group->get_id() ){
			unset( $group );
			break;
		}
	}
	if( isset($group) )
		$groups_available[] = array(
			'id' => $group->get_id(),
			'name' => $group->get_name()
		);
}
$smarty->assign( 'groups_available', $groups_available );



/* --------------------------------------- Groups ------------------------------------- */
if( isset($_REQUEST['group']) )
	$current_group = rail_factory::get_object( $rail, $_REQUEST['group'] );

// get all groups
$rail_groups = array();
$data = $rail->send_request( "SELECT `id` FROM `objects` WHERE `type`='RAIL_GROUP'" );
while( $data_group = $data->fetch_assoc() ){
	$group = rail_factory::get_object( $rail, $data_group['id'] );
	if( !isset($current_group) ) $current_group = $group;
	$rail_groups[] = array(
		'id' => $group->get_id(),
		'name' => $group->get_name(),
		'members_size' => $group->count_members()
	);
}
$smarty->assign( array(
	'rail_groups' => $rail_groups,
	'current_group' => array(
		'id' => $current_group->get_id(),
		'name' => $current_group->get_name(),
		'path' => $current_group->get_path(),
		'time_create' => date( 'D d.m.Y H:i', $current_group->get_attribute( OBJ_TIME_CREATE ) ),
		'time_lastwrite' => date( 'D d.m.Y H:i', $current_group->get_attribute( OBJ_TIME_LASTWRITE ) )
	)
));


/* -------------------------------------- Display ------------------------------------- */
$smarty->assign( array(

));
$smarty->display( 'users.tpl' );

?>